package com.juzi.wopu;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonHelp {
	
	public static String JosnGetString(JSONObject js,String key)
	{
		try {
			if (js.has(key))
				return js.getString(key);
			else
				return "";
		} catch (Exception e) {
			// TODO: handle exception
			return "";
		}
	}
	
	/*public static int JosnGetInt(JSONObject js,String key)
	{
		try {
			if (js.has(key))
				return js.getInt(key);
			else
				return -1;
		} catch (Exception e) {
			// TODO: handle exception
			return -1;
		}
	}*/
	
	public static String getJsonErr(String Code,String Desc) 
	{
		try
		{
			JSONObject js=new JSONObject(); 
			js.put("Code", Code);
			js.put("Desc", (Desc));
			String sjson=js.toString();
			return sjson; 
		}
		catch (Exception e){  
			e.printStackTrace(); 
	        String sErr=e.getMessage();  
	        return "{\"code\":\"999\", \"desc\":\""+(sErr)+"\"}";
	    }		
	}
	
	public static String getJsonData(String Code,String Desc,JSONObject JData) 
	{
		try
		{
			JSONObject js=new JSONObject(); 
			js.put("Code", Code);
			js.put("Desc", Desc);
			js.put("data", JData);
			String sjson=js.toString();
			return sjson; 
		}
		catch (Exception e){  
			e.printStackTrace(); 
	        String sErr=e.getMessage();  
	        return "{\"code\":\"999\", \"desc\":\""+sErr+"\"}";
	    }		
	}
	
	public static String getJsonTokenData(String Code,String Desc,String Token,JSONObject JData) 
	{
		try
		{
			JSONObject js=new JSONObject(); 
			js.put("Code", Code);
			js.put("Desc", Desc);
			js.put("Token", Token);
			js.put("data", JData);
			String sjson=js.toString();
			return sjson; 
		}
		catch (Exception e){  
			e.printStackTrace(); 
	        String sErr=e.getMessage();  
	        return "{\"code\":\"999\", \"desc\":\""+sErr+"\"}";
	    }		
	}
	
	public static JSONObject setJImg(String sThumbnailPath,String sImgPath)
	{
		
		
		JSONObject jImg = new JSONObject();
		try {
			jImg.put("sThumbnailPath", sThumbnailPath);
			jImg.put("sImgPath", sImgPath);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return jImg;
		
	}
	
	public static String getJsonStatus(String Code,String Desc,String Mobile,String ProductID,String ProductName,String ProductPrice,String Status,
			String CreateTime,String OpenTime,String Remarks,String GUID) 
	{
		try
		{
			JSONObject js=new JSONObject(); 
			js.put("Code", Code);
			js.put("Desc", Desc);
			if (GUID!=null)
				js.put("GUID", GUID);
			js.put("Mobile", Mobile);
			js.put("ProductID", ProductID);
			js.put("ProductName", ProductName);
			js.put("ProductPrice", ProductPrice);
			js.put("Status", Status);
			js.put("CreateTime", CreateTime);
			js.put("OpenTime", OpenTime);
			js.put("Remarks", Remarks);
			String sjson=js.toString();
			return sjson; 
		}
		catch (Exception e){  
			e.printStackTrace(); 
	        String sErr=e.getMessage();  
	        return "{\"code\":\"999\", \"desc\":\""+sErr+"\"}";
	    }		
	}

public static JSONObject getJsonHead(Connection con,String sMobile)
{
		String sSql="select tImg.sThumbnailPath,tImg.sImgPath  from tImg left join tOwner on tImg.iImgId=tOwner.iPortrait and tImg.iType=1 where tOwner.smobile='"+sMobile+"'";
		LoggerUtil.Log(sSql);
		ResultSet rs=BaseDB.OpenSql(con, sSql);
		String sThumbnailPath = null;
		String sImgPath = null;
		try {
			while(rs.next()){
				sThumbnailPath = rs.getString("sThumbnailPath");
				sImgPath=rs.getString("sImgPath");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    JSONObject headImg=setJImg(sThumbnailPath,sImgPath);
	    return headImg; 
}

}
